package com.whut.blog.controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;
//import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
//import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.whut.blog.entity.User;
import com.whut.blog.entity.UserT;
import com.whut.blog.service.IInfoSQL;
import com.whut.blog.service.IUserSQL;
import com.whut.blog.service.IUserService;
import com.whut.blog.utils.TokenUtils;
import com.whut.blog.utils.ImageToBase64;

import jakarta.annotation.Resource;

//import jakarta.annotation.Resource;

@Controller
@RestController
@CrossOrigin(origins = "*")
public class BlogController {
    @Resource
    private IUserService userService;

    @Resource
    private IUserSQL userSQL;

    @Resource
    private IInfoSQL infoSQL;//操作User表

    
    @RequestMapping(value="/addinfo",method=RequestMethod.POST)
    public Map<String, Object> addinfo(@RequestParam String date,@RequestParam String province,@RequestParam String city,@RequestParam String name,@RequestParam String address,@RequestParam String zip){

        Map<String, Object> map = new HashMap<>(3);
        //创建实体
        User user = new User();
        user.setDate(date);
        user.setProvince(province);
        user.setCity(city);
        user.setName(name);
        user.setAddress(address);
        user.setZip(zip);
        System.out.println(date);
        System.out.println(province);
        System.out.println(city);
        System.out.println(name);
        System.out.println(address);
        System.out.println(zip);
        //插入数据库
        try{
            System.out.println("插入数据库");
            infoSQL.save(user);
            map.put("data", "测试");
            map.put("code", 200);
            map.put("msg", "添加成功");
            return map;
        }
        catch(Exception e){
            System.out.println("插入数据库失败");
            map.put("data", "测试");
            map.put("code", 400);
            map.put("msg", "添加失败");
            return map;
        }
        


       
       
    }

    @RequestMapping(value="/updatepwd",method=RequestMethod.POST)
    public Map<String, Object> updatepwd(@RequestParam String name,@RequestParam String password,@RequestParam String newpwd,@RequestParam String ensurepwd){

        Map<String, Object> map = new HashMap<>(3);
        boolean res = userService.updatePwd(name, password, newpwd);
        if(res){
            map.put("data", "测试");
            map.put("code", 200);
            map.put("msg", "修改成功");
            return map;
        }
        else{
            map.put("data", "测试");
            map.put("code", 400);
            map.put("msg", "修改失败");
            return map;
        }
    }

    @RequestMapping(value = "/verify", method=RequestMethod.GET)
    public Map<String, Object> verify(@RequestParam String token) {
        boolean ok = TokenUtils.verify(token);
        Map<String, Object> map = new HashMap<>(3);
        if(ok){
            
            map.put("data", "测试");
            map.put("code", 200);
            map.put("msg", "验证成功");
            return map;
        }
        else{
            map.put("data", "测试");
            map.put("code", 400);
            map.put("msg", "验证失败");
            return map;
        }
    }
    
    @RequestMapping(value="/inithome",method=RequestMethod.GET)
    public Map<String, Object> inithome(@RequestParam String token){
        //从token中获取用户名
        String name = TokenUtils.get_payload(token);

        //从数据库中获取用户信息
        QueryWrapper<UserT> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        UserT user = userSQL.getOne(queryWrapper);
        Map<String, Object> map = new HashMap<>(3);
        try {
            user.setAvatar(ImageToBase64.encodeImageToBase64(user.getAvatar()));
            user.setPassword("");
        } catch (IOException e) {
            e.printStackTrace();
            map.put("data", "测试");
            map.put("code", 400);
            map.put("msg", "初始化失败");
            return map; 
        }

        //返回用户信息
       
        map.put("data", user);
        map.put("code", 200);
        map.put("msg", "初始化");
        return map; 
    }

    @RequestMapping(value="/login",method=RequestMethod.GET)
    public Map<String, Object> login(@RequestParam String name,@RequestParam String password){

        Map<String, Object> map = new HashMap<>(3);
  
        
        if(userService.userLogin(name, password)){
            //生成token
            String tok = TokenUtils.token(name, password);

            map.put("data", tok);
            map.put("code", 200);
            map.put("msg", "登录成功");
        }
        else{
            map.put("data", "测试");
            map.put("code", 400);
            map.put("msg", "用户名或密码错误");
        }
        return map;
    }

    
    @RequestMapping(value="/loginPost",method=RequestMethod.POST)
    public Map<String, Object> loginpost(@RequestBody UserT us){

        Map<String, Object> map = new HashMap<>(3);        
        System.out.println("接收到post参数");
        System.out.println(us.getName());
        System.out.println(us.getEmail());
        System.out.println(us.getPassword());
        
        map.put("data", "测试");
        if(userService.userLogin(us.getName(), us.getPassword())){
            map.put("code", 200);
            map.put("msg", "登录成功");
        }
        else{
            map.put("code", 400);
            map.put("msg", "登录失败");
        }
        return map;
    }

    @RequestMapping(value="/loginSave",method=RequestMethod.POST)
    public Map<String, Object> loginsave(@RequestBody UserT us){

        Map<String, Object> map = new HashMap<>(3);
        
        System.out.println("接收到post参数");
        System.out.println(us.getName());
        System.out.println(us.getEmail());
        System.out.println(us.getPassword());
        userSQL.save(us);
        map.put("data", "测试");
        if(userService.userLogin(us.getName(), us.getPassword())){
            map.put("code", 200);
            map.put("msg", "登录成功");
        }
        else{
            map.put("code", 400);
            map.put("msg", "登录失败");
        }
        return map;
    }

    @RequestMapping(value="/regist",method=RequestMethod.POST)
    public Map<String, Object> regist(@RequestParam MultipartFile avatar, @RequestParam String name, @RequestParam String password, @RequestParam String email, @RequestParam String birthday){
        boolean res = userService.userRegister(avatar, name, password, email, birthday);
        Map<String, Object> map = new HashMap<>(3);
        if(res){
            map.put("data", "测试");
            map.put("code", 200);
            map.put("msg", "注册成功");
            return map;
        }
        else{
            map.put("data", "测试");
            map.put("code", 400);
            map.put("msg", "注册失败");
            return map;
        }
         
      
    }

}